﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WhatCatch.Core2
{
    public class TestHttpLoader : IHttpLoader
    {
        /// <summary>
        /// 最大页数
        /// </summary>
        readonly static int MaxIndex = 10;
        /// <summary>
        /// 每页项数
        /// </summary>
        readonly static int ListSize = 20;

        /*
         
            http://www.test.com/page/1
            <html>
            <head>page1</head>
            <body>
            <div class=""list"">
                <div class=""list-li""><a href=""http://www.test.com/1"">xxxxxxxx</a></div>
                <div class=""list-li""><a href=""http://www.test.com/2"">xxxxxxxx</a></div>
                <div class=""list-li""><a href=""http://www.test.com/3"">xxxxxxxx</a></div>
                <div class=""list-li""><a href=""http://www.test.com/4"">xxxxxxxx</a></div>
            </div>
            <div class=""page"">
                <a href=""http://www.test.com/page/1"">1</a>
                <a href=""http://www.test.com/page/2"">1</a>
                <a href=""http://www.test.com/page/3"">1</a>
            </div>
            </body>
            </html>

            http://www.test.com/detail/1
            <html>
            <head>detail1</head>
            <body>
            <div class=""detail"">
                <div class=""detail-title"">xxxxxxxx</div>
                <div class=""detail-date"">2019-08-09</div>
                <div class=""detail-content"">xxxxxxxx</div>
            </div>
            </body>
            </html>
             */



        private string GetListPage(int index)
        {
            //模板
            string html = @"<html>
            <head><title>【title】</title></head>
            <body>
            <div class=""list"">
                【list】
            </div>
            <div class=""page"">
                【page】
            </div>
            </body>
            </html>";

            html = html.Replace("【title】", "page" + index);

            //列表
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ListSize; i++)
            {
                sb.AppendLine($@"<div class=""list-li""><a href=""http://www.test.com/detail/{Guid.NewGuid().ToString()}/{(index - 1) * ListSize + i + 1}"">{Guid.NewGuid().ToString()}</a></div>");
            }
            html = html.Replace("【list】", sb.ToString());

            //分页
            sb.Clear();
            for (int i = 0; i < 10; i++)
            {
                if (index + i > MaxIndex)
                    break;
                sb.AppendLine($@"<a href=""http://www.test.com/page/{index + i}"">{index + i}</a>");
            }
            html = html.Replace("【page】", sb.ToString());

            return html;
        }

        private string GetDetailPage(int id)
        {
            string html = @"<html>
            <head><title>【title】</title></head>
            <body>
            <div class=""detail"">
                <div class=""detail-title"">【title】</div>
                <div class=""detail-date"">【date】</div>
                <div class=""detail-content"">【content】</div>
            </div>
            </body>
            </html>";
            html = html.Replace("【title】", "detail" + id)
                .Replace("【date】", DateTime.Now.AddDays(-id).ToString("yyyy-MM-dd"))
                .Replace("【content】", Guid.NewGuid().ToString());

            return html;
        }

        /// <summary>
        /// 获取页面
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetHtml(string url)
        {
            Thread.Sleep(200);

            string str = url.Substring(url.LastIndexOf('/') + 1);
            int num = int.Parse(str);
            if (url.Contains("page"))
            {
                return GetListPage(num);
            }
            else if (url.Contains("detail"))
            {
                return GetDetailPage(num);
            }
            else
            {
                return "undefault";
            }
        }

    }

    public class Page
    {
        public List<string> Details { get; set; }
    }
}
